package org.gdstash.ui.character;

import org.gdstash.character.GDChar;
import org.gdstash.character.GDCharUID;
import org.gdstash.ui.util.GDAbstractRiftPane;
import org.gdstash.ui.util.GDAbstractShrinePane;
import org.gdstash.ui.util.GDAbstractUpdateCharPane;

import javax.swing.*;
import javax.swing.border.Border;
import java.util.LinkedList;
import java.util.List;

public class RoTRiftNShrinePane extends GDAbstractUpdateCharPane {
    private GDAbstractRiftPane pnlRift1;
    private GDAbstractRiftPane pnlRift2;
    private GDAbstractRiftPane pnlRift3;
    private GDAbstractRiftPane pnlRift4;
    private GDAbstractRiftPane pnlRift5;
    private GDAbstractRiftPane pnlRift6;
    private GDAbstractShrinePane pnlShrine1;
    private GDAbstractShrinePane pnlShrine2;
    private GDAbstractShrinePane pnlShrine3;
    private GDAbstractShrinePane pnlShrine4;
    private GDAbstractShrinePane pnlShrine5;
    private GDAbstractShrinePane pnlShrine6;
    private int difficulty;

    public RoTRiftNShrinePane(final int difficulty) {
        this.difficulty = difficulty;
        this.adjustUI();
        final JPanel pnlMain = this.buildMainPanel();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(false);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(pnlMain));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(pnlMain));
        layout.setVerticalGroup(vGroup);
    }

    @Override
    public void adjustUI() {
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        this.setBorder(compound);
        if (this.pnlRift1 == null) {
            this.pnlRift1 = new RoTRiftAct1Pane(this.difficulty, 1);
        } else {
            this.pnlRift1.adjustUI();
        }
        if (this.pnlRift2 == null) {
            this.pnlRift2 = new RoTRiftAct2Pane(this.difficulty, 1);
        } else {
            this.pnlRift2.adjustUI();
        }
        if (this.pnlRift3 == null) {
            this.pnlRift3 = new RoTRiftAct3Pane(this.difficulty, 1);
        } else {
            this.pnlRift3.adjustUI();
        }
        if (this.pnlRift4 == null) {
            this.pnlRift4 = new RoTRiftAct4Pane(this.difficulty, 1);
        } else {
            this.pnlRift4.adjustUI();
        }
        if (this.pnlRift5 == null) {
            this.pnlRift5 = new RoTRiftAct5Pane(this.difficulty, 1);
        } else {
            this.pnlRift5.adjustUI();
        }
        if (this.pnlRift6 == null) {
            this.pnlRift6 = new RoTRiftAct6Pane(this.difficulty, 1);
        } else {
            this.pnlRift6.adjustUI();
        }
        if (this.pnlShrine1 == null) {
            this.pnlShrine1 = new GDCharShrineAct1Pane(this.difficulty, 1);
        } else {
            this.pnlShrine1.adjustUI();
        }
        if (this.pnlShrine2 == null) {
            this.pnlShrine2 = new GDCharShrineAct2Pane(this.difficulty, 1);
        } else {
            this.pnlShrine2.adjustUI();
        }
        if (this.pnlShrine3 == null) {
            this.pnlShrine3 = new GDCharShrineAct3Pane(this.difficulty, 1);
        } else {
            this.pnlShrine3.adjustUI();
        }
        if (this.pnlShrine4 == null) {
            this.pnlShrine4 = new GDCharShrineAct4Pane(this.difficulty, 1);
        } else {
            this.pnlShrine4.adjustUI();
        }
        if (this.pnlShrine5 == null) {
            this.pnlShrine5 = new GDCharShrineAct5Pane(this.difficulty, 1);
        } else {
            this.pnlShrine5.adjustUI();
        }
        if (this.pnlShrine6 == null) {
            this.pnlShrine6 = new GDCharShrineAct6Pane(this.difficulty, 1);
        } else {
            this.pnlShrine6.adjustUI();
        }
    }

    private JPanel buildMainPanel() {
        final JPanel panel = new JPanel();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutoCreateGaps(false);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlRift1).addComponent(this.pnlShrine1)).addGroup(layout.createParallelGroup().addComponent(this.pnlRift2).addComponent(this.pnlShrine2)).addGroup(layout.createParallelGroup().addComponent(this.pnlRift3).addComponent(this.pnlShrine3)).addGroup(layout.createParallelGroup().addComponent(this.pnlRift4).addComponent(this.pnlShrine4)).addGroup(layout.createParallelGroup().addComponent(this.pnlRift5).addComponent(this.pnlShrine5)).addGroup(layout.createParallelGroup().addComponent(this.pnlRift6).addComponent(this.pnlShrine6));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlRift1).addComponent(this.pnlRift2).addComponent(this.pnlRift3).addComponent(this.pnlRift4).addComponent(this.pnlRift5).addComponent(this.pnlRift6)).addGroup(layout.createParallelGroup().addComponent(this.pnlShrine1).addComponent(this.pnlShrine2).addComponent(this.pnlShrine3).addComponent(this.pnlShrine4).addComponent(this.pnlShrine5).addComponent(this.pnlShrine6));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(0, this.pnlRift1, this.pnlRift2);
        layout.linkSize(0, this.pnlRift1, this.pnlRift3);
        layout.linkSize(0, this.pnlRift1, this.pnlRift4);
        layout.linkSize(0, this.pnlRift1, this.pnlRift5);
        layout.linkSize(0, this.pnlRift1, this.pnlRift6);
        layout.linkSize(0, this.pnlRift1, this.pnlShrine1);
        layout.linkSize(0, this.pnlRift1, this.pnlShrine2);
        layout.linkSize(0, this.pnlRift1, this.pnlShrine3);
        layout.linkSize(0, this.pnlRift1, this.pnlShrine4);
        layout.linkSize(0, this.pnlRift1, this.pnlShrine5);
        layout.linkSize(0, this.pnlRift1, this.pnlShrine6);
        layout.linkSize(1, this.pnlRift1, this.pnlRift2);
        layout.linkSize(1, this.pnlRift1, this.pnlRift3);
        layout.linkSize(1, this.pnlRift1, this.pnlRift4);
        layout.linkSize(1, this.pnlRift1, this.pnlRift5);
        layout.linkSize(1, this.pnlRift1, this.pnlRift6);
        layout.linkSize(1, this.pnlShrine1, this.pnlShrine2);
        layout.linkSize(1, this.pnlShrine1, this.pnlShrine3);
        layout.linkSize(1, this.pnlShrine1, this.pnlShrine4);
        layout.linkSize(1, this.pnlShrine1, this.pnlShrine5);
        layout.linkSize(1, this.pnlShrine1, this.pnlShrine6);
        return panel;
    }

    public List<GDCharUID> getRiftList(final boolean found) {
        final List<GDCharUID> listAll = new LinkedList<GDCharUID>();
        List<GDCharUID> list = null;
        list = this.pnlRift1.getRiftList(found);
        if (list != null) {
            listAll.addAll(list);
        }
        list = this.pnlRift2.getRiftList(found);
        if (list != null) {
            listAll.addAll(list);
        }
        list = this.pnlRift3.getRiftList(found);
        if (list != null) {
            listAll.addAll(list);
        }
        list = this.pnlRift4.getRiftList(found);
        if (list != null) {
            listAll.addAll(list);
        }
        list = this.pnlRift5.getRiftList(found);
        if (list != null) {
            listAll.addAll(list);
        }
        list = this.pnlRift6.getRiftList(found);
        if (list != null) {
            listAll.addAll(list);
        }
        return listAll;
    }

    public List<GDCharUID> getShrineList(final boolean found) {
        final List<GDCharUID> listAll = new LinkedList<GDCharUID>();
        List<GDCharUID> list = null;
        list = this.pnlShrine1.getShrineList(found);
        if (list != null) {
            listAll.addAll(list);
        }
        list = this.pnlShrine2.getShrineList(found);
        if (list != null) {
            listAll.addAll(list);
        }
        list = this.pnlShrine3.getShrineList(found);
        if (list != null) {
            listAll.addAll(list);
        }
        list = this.pnlShrine4.getShrineList(found);
        if (list != null) {
            listAll.addAll(list);
        }
        list = this.pnlShrine5.getShrineList(found);
        if (list != null) {
            listAll.addAll(list);
        }
        list = this.pnlShrine6.getShrineList(found);
        if (list != null) {
            listAll.addAll(list);
        }
        return listAll;
    }

    @Override
    public void setChar(final GDChar gdc) {
        this.pnlRift1.setChar(gdc);
        this.pnlRift2.setChar(gdc);
        this.pnlRift3.setChar(gdc);
        this.pnlRift4.setChar(gdc);
        this.pnlRift5.setChar(gdc);
        this.pnlRift6.setChar(gdc);
        this.pnlShrine1.setChar(gdc);
        this.pnlShrine2.setChar(gdc);
        this.pnlShrine3.setChar(gdc);
        this.pnlShrine4.setChar(gdc);
        this.pnlShrine5.setChar(gdc);
        this.pnlShrine6.setChar(gdc);
    }

    @Override
    public void updateChar(final GDChar gdc) {
        if (gdc == null) {
            return;
        }
        boolean changed = false;
        changed = (changed || this.pnlRift1.hasChanged());
        changed = (changed || this.pnlRift2.hasChanged());
        changed = (changed || this.pnlRift3.hasChanged());
        changed = (changed || this.pnlRift4.hasChanged());
        changed = (changed || this.pnlRift5.hasChanged());
        changed = (changed || this.pnlRift6.hasChanged());
        changed = (changed || this.pnlShrine1.hasChanged());
        changed = (changed || this.pnlShrine2.hasChanged());
        changed = (changed || this.pnlShrine3.hasChanged());
        changed = (changed || this.pnlShrine4.hasChanged());
        changed = (changed || this.pnlShrine5.hasChanged());
        changed = (changed || this.pnlShrine6.hasChanged());
        if (!changed) {
            return;
        }
        final List<GDCharUID> listAddRifts = this.getRiftList(true);
        final List<GDCharUID> listRemoveRifts = this.getRiftList(false);
        gdc.setRiftList(this.difficulty, listAddRifts, listRemoveRifts);
        final List<GDCharUID> listAddShrines = this.getShrineList(true);
        final List<GDCharUID> listRemoveShrines = this.getShrineList(false);
        gdc.setRestoredShrinesList(this.difficulty, listAddRifts, listRemoveRifts);
        this.pnlRift1.setChanged(false);
        this.pnlRift2.setChanged(false);
        this.pnlRift3.setChanged(false);
        this.pnlRift4.setChanged(false);
        this.pnlRift5.setChanged(false);
        this.pnlRift6.setChanged(false);
        this.pnlShrine1.setChanged(false);
        this.pnlShrine2.setChanged(false);
        this.pnlShrine3.setChanged(false);
        this.pnlShrine4.setChanged(false);
        this.pnlShrine5.setChanged(false);
        this.pnlShrine6.setChanged(false);
    }
}
